package com.tecsup.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import com.tecsup.excepcion.DAOExcepcion;
import com.tecsup.util.ConexionBD;
import com.tecsup.com.modelo.Pronosticos;
import com.tecsup.dao.BaseDAO;

public class PronosticosDAO extends BaseDAO{

	public void insertar(Pronosticos vo) throws DAOExcepcion{
		System.out.println("PronosticoDAO: insertar(Pronostico vo)");
		String query = "INSERT INTO pronosticos(producto, periodo, demanda, pronostico) VALUES (?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try{
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getProducto());
			stmt.setString(2, vo.getPeriodo());
			stmt.setInt(3, vo.getDemanda());
			stmt.setDouble(4, vo.getPronostico());
			int i = stmt.executeUpdate();
			if (i != 1){
				throw new SQLException("No se pudo insertar");
			}
		}catch (SQLException e){
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}finally{
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	
	public void actualizar(Pronosticos vo) throws DAOExcepcion{
		System.out.println("PronosticoDAO: actualizar(Pronostico vo)");
		String query = "update pronosticos set producto=?, periodo=?, demanda=?, pronostico=? where idPronostico=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try{
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getProducto());
			stmt.setString(2, vo.getPeriodo());
			stmt.setInt(3, vo.getDemanda());
			stmt.setDouble(4, vo.getPronostico());
			stmt.setInt(5, vo.getIdPronostico());
			int i = stmt.executeUpdate();
			if (i != 1){
				throw new SQLException("No se pudo actualizar");
			}
		}catch (SQLException e){
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}finally{
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	
	public Collection<Pronosticos> buscarPorProducto(String producto) throws DAOExcepcion{
		System.out.println("PronosticosDAO: buscarPorProducto(String producto)");
		String query = "SELECT producto, periodo, demanda, pronostico FROM pronosticos WHERE producto like ?";
		Collection<Pronosticos> lista = new ArrayList<Pronosticos>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try{
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + producto + "%");
			rs = stmt.executeQuery();
			while (rs.next()){
				Pronosticos vo = new Pronosticos();
				vo.setProducto(rs.getString("producto"));
				vo.setPeriodo(rs.getString("periodo"));
				vo.setDemanda(rs.getInt("demanda"));
				vo.setPronostico(rs.getDouble("pronostico"));
				lista.add(vo);
			}
		}catch (SQLException e){
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}finally{
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}
	
	public Collection<Pronosticos> listar() throws DAOExcepcion{
		System.out.println("PronosticosDAO: listar()");
		Collection<Pronosticos> c = new ArrayList<Pronosticos>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try{
			con = ConexionBD.obtenerConexion();
			String query = "SELECT producto, periodo, demanda, pronostico FROM pronosticos";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()){
				Pronosticos vo = new Pronosticos();
				vo.setProducto(rs.getString("producto"));
				vo.setPeriodo(rs.getString("periodo"));
				vo.setDemanda(rs.getInt("demanda"));
				vo.setPronostico(rs.getDouble("pronostico"));
				c.add(vo);
			}
		}catch (SQLException e){
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}finally{
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	public Pronosticos obtener(int idPronostico) throws DAOExcepcion{
		System.out.println("PronosticosDAO: obtener(String idPronosticos)");
		Pronosticos vo = new Pronosticos();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try{
			String query = "SELECT producto, periodo, demanda, pronostico FROM pronosticos WHERE idPronostico=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idPronostico);
			rs = stmt.executeQuery();
			if (rs.next()){
				vo.setIdPronostico(rs.getInt(1));
				vo.setProducto(rs.getString(2));
				vo.setPeriodo(rs.getString(3));
				vo.setDemanda(rs.getInt(4));
				vo.setPronostico(rs.getDouble(5));
			}
		}catch (SQLException e){
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}finally{
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
}
